let token = ''||localStorage.getItem('token');
//添加小贴士
export const upArticle = ()=> {
    let fm = new FormData(document.getElementById('form1'));
    let reg1 = /^[A-Za-z0-9]+$/img;
    let reg2 = /^[A-Za-z0-9]+$/img;
    let reg3 = /^[A-Za-z0-9]+$/img;
    let title = fm.get('title');
    let image = fm.get('image').name.split('.')[0];
    let content = fm.get('content').name.split('.')[0];
    let voice = fm.get('voice').name.split('.')[0];
    let legitimateImg = reg1.test(image);
    let legitimateCon = reg2.test(content);
    let legitimateVoi = reg3.test(voice);
    if(title.length!==0){
        if(legitimateImg){
            if(legitimateCon){
                if(legitimateVoi){
                    fetch('http://211.159.166.29:1234/admin/uploadpassage', {
                        method : 'POST',
                        mode : 'cors',
                        headers: {
                            'authorization':token
                        },
                        body : fm
                    }).then(res => {
                        if (res.ok) {
                            console.log(123);
                            //只有请求成功才会返回数据
                            return res.text()
                        } else {
                            throw new Error(`status: ${res.status}`)
                        }
                        }, err => {

                    }).then(d => {
                        if (d === undefined || d === '') {
                            return
                        }
                        setInterval(()=>{
                            window.history.go(0);
                        },500)
                    })
                    .catch(err => {
                        console.log(err)
                    })
                }else{
                    alert('音频文件名要求为英文字母或数字！')
                }
            }else{
                alert('文章文件名要求为英文字母或数字！');
                return ;
            }
        }else{
            alert('图片名要求为英文字母或数字！');
            return;
        }
    }else{
        alert('题目不能为空');
    }
}

//添加APP
export const addApp = (t)=> {
    let fileInfo = t.target.files;
    let fileName = fileInfo[0].name.split('.')[0];
    let reg = /^[A-Za-z][A-Za-z0-9]+$/img;
    if(reg.test(fileName)){
        if (t.target.files.length === 0) {
            return
        }
        let img = t.target.files[0],
            fm = new FormData();
        let cname = window.prompt('请输入APP名：');

        fm.append('image', img);
        if(cname!==null){
            fm.append('cname',cname);
            fetch('http://211.159.166.29:1234/admin/uploadapp', {
                method : 'POST',
                mode : 'cors',
                headers: {
                    'authorization':token
                },
                body : fm
            }).then(res => {
                if (res.ok) {
                    console.log(123);
                    //只有请求成功才会返回数据
                    setInterval(()=>{
                        window.history.go(0);
                    },500);
                    return res.text();
                    
                } else {
                    throw new Error(`status: ${res.status}`)
                }
                }, err => {
            }).then(d => {
                if (d === undefined || d === '') {
                    return
                }
                
            })
            .catch(err => {
                console.log(err)
            })
        }else{
            console.log('取消添加');
        }
    }else{
        alert('文件名要求英文字母开头的字母和数字的组合！');
    }
}

//添加App功能
export const addFun = (t,funName)=> {
    console.log(t,funName);
    var word = prompt("请输入功能");
    var str=t.target.id.split('/')[2];
    console.log(str);
    if (word === null || word === '') {
        return;
    }
    var data={
        "name":word,
        "appname":str,
        "ename":funName
    }
    fetch('http://211.159.166.29:1234/admin/uploadappFunc', {
        method : 'POST',
        mode : 'cors',
        headers: {
            'authorization':token
        },
        body : JSON.stringify(data)
    }).then(res => {
        if (res.ok) {
            console.log(123);
            //只有请求成功才会返回数据
            setInterval(()=>{
                window.history.go(0);
            },500)
            return res.text()
        } else {
            throw new Error(`status: ${res.status}`)
        }
        }, err => {

    }).then(d => {
        if (d === undefined || d === '') {
            return
        }
        
    })
    .catch(err => {
        console.log(err)
    })

}

//添加步骤
export const addStep = (t)=> {
    let fm1 = new FormData(document.getElementById('form2'));
    let fileName = fm1.get('file').name.split('.')[0];
    console.log(fileName);
    let ptArr=t.target.id.split('/');
    let pt = ptArr[2]+'_'+ptArr[3];
    let reg = /^[A-Za-z0-9]+$/img;
    if(reg.test(fileName)){
        fm1.append('table',pt);
        fetch('http://211.159.166.29:1234/admin/uploadappStep', {
            method : 'POST',
            mode : 'cors',
            headers: {
                'authorization':token
            },
            body : fm1
        }).then(res => {
            if (res.ok) {
                //只有请求成功才会返回数据
                setInterval(()=>{
                    window.history.go(0);
                },500)
                return res.text()
            } else {
                throw new Error(`status: ${res.status}`)
            }
            }, err => {

        }).then(d => {
            if (d === undefined || d === '') {
                return
            }
            
        })
        .catch(err => {
            console.log(err)
        })
    }else{
        alert('图片名要求为英文字母或数字！');
    }
}
